<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: EmployeeController.php 4929 2010-08-25 03:36:50Z jam-gate.vn $
 * @since		2.0.0
 */

class Employee_EmployeeController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new employee
	 * 
	 * @return void
	 */
	public function addAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$employeeDao = Tomato_Model_Dao_Factory::getInstance()->setModule('employee')->getEmployeeDao();
		$employeeDao->setDbConnection($conn);
		
		$nextId 	= 	$employeeDao->getNextId();
		$this->view->assign("nextId",$nextId);
		
		$employee = new stdClass();
		$employee->id		 		 = $nextId;
		$employee->shop_id 			 = Zend_Auth::getInstance()->getIdentity()->shop->id;
		$employee->employee_name_vi_VN = Tomato_Config::getConfig()->get("jam")->employee_name_vi_VN;
		$employee->employee_name_en_US = Tomato_Config::getConfig()->get("jam")->employee_name_en_US;
		$employee->amount			 = 0;
		$employee->original_price	 = 0;
		$employee->sale_price		 = 0;
		$employee->employee_unit		 = "";
		$employee->employee_unit_use   = "";
		$employee->volume_one 		 = 1;
		$employee->images 		 	 = "";
		$employee->note_used			 = "";
		$request  = $this->getRequest();
		
		$this->view->assign('lang', $request->getParam('lang'));
		
		if ($request->isPost()) {
			$user 	  = Zend_Auth::getInstance()->getIdentity();
			$employee->employee_name_vi_VN = $request->getParam("employee_name_vi_VN",$employee->employee_name_vi_VN);
			$employee->employee_name_en_US = $request->getParam("employee_name_en_US",$employee->employee_name_en_US);
			$employee->amount			 = $request->getParam("amount",$employee->amount);
			$employee->original_price	 = $request->getParam("original_price",$employee->original_price);
			$employee->sale_price		 = $request->getParam("sale_price",$employee->sale_price);
			$employee->employee_unit		 = $request->getParam("employee_unit",$employee->employee_unit);
			$employee->employee_unit_use   = $request->getParam("employee_unit_use",$employee->employee_unit_use);
			$employee->volume_one 		 = $request->getParam("volume_one",$employee->volume_one);
			$employee->volume_total 		 = $request->getParam("volume_total",$employee->volume_one * $employee->amount);
			$employee->images 		 	 = $request->getParam("images",$employee->images);
			$employee->note_used 		 = $request->getParam("note_used",$employee->note_used);
			$id = $employeeDao->add($employee);			
			$this->_helper->getHelper('FlashMessenger')
				->addMessage($this->view->translator('employee_add_success'));
			$this->_redirect($this->view->serverUrl() . $this->view->url(array("employee_id"=>$employee->id), 'employee_employee_edit'));
		}
		$this->view->assign("employee",$employee);
	}
	
	/**
	 * Delete employee
	 * 
	 * @return void
	 */
	public function deleteAction() 
	{
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		
		$response = 'RESULT_ERROR';
		$request  = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('id');
			$conn = Tomato_Db_Connection::factory()->getMasterConnection();
			$employeeDao = Tomato_Model_Dao_Factory::getInstance()->setModule('employee')->getEmployeeDao();
			$employeeDao->setDbConnection($conn);
			$employee = $employeeDao->getById($id);
			
			if ($employee != null) {
				// delete resource file				
				$dir  = 'resource'.DS.substr(Tomato_Config::getConfig()->get("db")->prefix,0,-1).DS.$employee->shop_id.DS.'images'.DS.'employees'.DS.$employee->id; 
				if(file_exists($dir)){
					Tomato_Utility_File::deleteRescursiveDir(TOMATO_ROOT_DIR.DS.$dir);
				}
				$employeeDao->delete($employee);
				$response = 'RESULT_OK';
			}
		}
		$this->getResponse()->setBody($response);
	}
	
	/**
	 * Edit employee
	 * 
	 * @return void
	 */
	public function editAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$employeeDao = Tomato_Model_Dao_Factory::getInstance()->setModule('employee')->getEmployeeDao();
		$employeeDao->setDbConnection($conn);
		$request  = $this->getRequest();		
		$employee_id = $request->getParam("employee_id",null);
		if($employee_id){
			$employee 	= 	$employeeDao->getById($employee_id);
			if($employee){				
				$this->view->assign('lang', $request->getParam('lang'));
				
				if ($request->isPost()) {
					$user 	  = Zend_Auth::getInstance()->getIdentity();
					$employee->employee_name_vi_VN = $request->getParam("employee_name_vi_VN",$employee->employee_name_vi_VN);
					$employee->employee_name_en_US = $request->getParam("employee_name_en_US",$employee->employee_name_en_US);
					$employee->amount			 = $request->getParam("amount",$employee->amount);
					$employee->original_price	 = $request->getParam("original_price",$employee->original_price);
					$employee->sale_price		 = $request->getParam("sale_price",$employee->sale_price);
					$employee->employee_unit		 = $request->getParam("employee_unit",$employee->employee_unit);
					$employee->employee_unit_use   = $request->getParam("employee_unit_use",$employee->employee_unit_use);
					$employee->volume_one 		 = $request->getParam("volume_one",$employee->volume_one);
					$employee->volume_total 		 = $request->getParam("volume_total",$employee->volume_one * $employee->amount);
					$employee->images 		 	 = $request->getParam("images",$employee->images);
					$employee->note_used			 = $request->getParam("note_used",$employee->note_used);
						
					$id = $employeeDao->update($employee);			
					$this->_helper->getHelper('FlashMessenger')
						->addMessage($this->view->translator('employee_edit_success'));
					$this->_redirect($this->view->serverUrl() . $this->view->url(array("employee_id"=>$employee->id), 'employee_employee_edit'));
				}
				$this->view->assign("employee",$employee);
			}else{
				Core_Services_Deny::deny($this->view->translator("employee_deny_id_non_exists"));
			}
		}
	}
	
	/**
	 * List categories
	 * 
	 * @return void
	 */
	public function listAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$employeeDao = Tomato_Model_Dao_Factory::getInstance()->setModule('employee')->getEmployeeDao();
		$employeeDao->setDbConnection($conn);
		
		$lang = $this->getRequest()->getParam('lang', Tomato_Config::getConfig()->web->lang);
		
		$employeeDao->setLang($lang);
        $request   = $this->getRequest();
		$pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = Tomato_Config::getConfig()->manager->itemPerPage;;
        $offset    = ($pageIndex - 1) * $perPage;
        $exp = array(
        	   );
		$employees 	 = $employeeDao->find($offset, $perPage, $exp);
		$this->view->assign('employees', $employees);
		
		$total       = $employeeDao->count($exp);        
		$paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($employees, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
        
		$this->view->assign('paginator', $paginator);
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url(array(), 'employee_employee_list'),
            'itemLink' 	  => 'page-%d'                          
        ));
        $this->view->assign('lang',$lang);				
	} 		
}	